package at.fhj.mail;

import java.util.Properties;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@Named("mailBean")
@RequestScoped
public class GMail {

	// properties file machen
	final String username = "kevin.ballach@gmail.com";
	final String password = "nachtfalke";
	// singelton bauen
	// private static final GMail MAILSERVICE = null;

	private static Session session;
	private String receiver;

	public GMail() {

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		session = Session.getInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});
	}

	public static void sendMessage(String receiver) throws MessagingException {
		Message message = new MimeMessage(session);

		message.setFrom(new InternetAddress("kevin.ballach@gmail.com"));
		message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(receiver));
		message.setSubject("Test");
		message.setText("ADFJSDFHSDFHSDHFSDHGFHHGH");

		Transport.send(message);
		System.out.println("DO NE");

	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

}

/*
 * try {
 * 
 * Message message = new MimeMessage(session); message.setFrom(new
 * InternetAddress("kevin.ballach@gmail.com"));
 * message.setRecipients(Message.RecipientType.TO,
 * InternetAddress.parse("@gmail.com")); message.setSubject("Testing Subject");
 * message.setText("TEST TEST TEST TTEST");
 * 
 * Transport.send(message);
 * 
 * System.out.println("Done");
 * 
 * } catch (MessagingException e) { throw new RuntimeException(e); } } }
 */